package com.queen.restaurant.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.MappingJsonFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.queen.restaurant.base.BaseMessage;
import com.queen.restaurant.domain.User;
import com.queen.restaurant.service.UserService;
import com.queen.restaurant.util.BaiduGeoUtil;
import com.queen.restaurant.util.HttpClientUtil;

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired(required=true)
	private UserService userService;
	
	@RequestMapping("/register")
	public String register(){
		return "user/register";
	}
	
	@ModelAttribute("user")
	public User getUser(){
		return new User();
	}
	
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userLogin(
			@RequestParam("username") String username,
			@RequestParam("password") String password
			){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = BaseMessage.ERROR;
		String message = "";
		User user = userService.userLogin(username);
		if (user == null) {
			message = "用户不存在，请先注册";
		}else if (!user.getPassword().equals(password)) {
			message = "密码错误，请重新输入";
			user = null;
		}else {
			result = BaseMessage.SUCCESS;
			message = "成功";
		}
		
		resultMap.put("result", result);
		resultMap.put("user", user);
		resultMap.put("message", message);
		
		return resultMap;
	}
	
	@RequestMapping(value="/register",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userRegister(
			@RequestParam("username") String username,
			@RequestParam("password") String password
			){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		User user = null;
		int result = BaseMessage.ERROR;
		String message = "";
		try {
			user = userService.userLogin(username);
			if (user != null) {
				message = "用户已存在";
				user = null;
			}else {
				user = new User();
				user.setUserName(username);
				user.setPassword(password);
				userService.createUser(user);
				
				String baiduResult = craeteNewUserPoi(user);
				System.out.println("baiduResult = " + baiduResult);
				if (baiduResult.equals("error")) {
					message = "访问出错";
				} else {
					String userPoiId = BaiduGeoUtil.createPoiResult(baiduResult);
					if (!userPoiId.equals("")) {
						user.setUserPoiId(userPoiId);
						userService.updateUser(user);
						result = BaseMessage.SUCCESS;
						message = "成功";
					}
				}
			}
		} catch (Exception e) {
			message = "访问出错，请重新提交数据";
		}finally{
			resultMap.put("result", result);
			resultMap.put("user", user);
			resultMap.put("message", message);
		}
		return resultMap;
	}

	@RequestMapping(value="/create",method=RequestMethod.POST)
	public ModelAndView createNew(@ModelAttribute("user")User user){
		ModelAndView mav = new ModelAndView();
		String result = "user/createFail";
		try {
//			SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
//			Date date = myFormatter.parse("1985-04-24");
			user.setBirthday("1985-04-24");
			user.setLevel(0);
			user.setCredit(0);
			userService.createUser(user);
			
			String baiduResult = craeteNewUserPoi(user);
			System.out.println("baiduResult = " + baiduResult);
			if (baiduResult.equals("error")) {
				result = "访问出错";
			} else {
				String userPoiId = BaiduGeoUtil.createPoiResult(baiduResult);
				if (!userPoiId.equals("")) {
					user.setUserPoiId(userPoiId);
					userService.updateUser(user);
					result = "user/createSuccess";
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		mav.addObject(user).setViewName(result);
		return mav;
//		ModelAndView mav = new ModelAndView();
//		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
//		Date date;
//		try {
//			date = myFormatter.parse("1985-04-24");
//			User user = new User("liukun", "123", "男", "123456", date, 0, 0);
//			userService.createUser(user);
//			mav.setViewName("user/createSuccess");
//			mav.addObject("user",user);
//		} catch (ParseException e) {
//			e.printStackTrace();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return mav;
	}
	
	//创建用户POI数据
	public String craeteNewUserPoi(User user){
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("geotable_id", BaiduGeoUtil.USER_POI_GEOTABLE_ID);
		paramMap.put("ak", BaiduGeoUtil.AK);
		paramMap.put("coord_type", 3);
		paramMap.put("longitude", 0);
		paramMap.put("latitude", 0);
		paramMap.put("title", user.getUserName());

		paramMap.put("user_id", user.getId());
		paramMap.put("user_name", user.getUserName());
		paramMap.put("gender", user.getGender());
		paramMap.put("birthday", user.getBirthday());
		paramMap.put("header_img_url", "");
		
		//paramMap.put("tags", "");
		return HttpClientUtil.getHttpPostResult(BaiduGeoUtil.CREATE_POI_URL, paramMap);
	}
	
	
}
